﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace Labirinto_Magico
{
    public class GUIButton
    {
        public String mType;
        public Vector2 mPosition;
        public Vector2 mDimension;
        public bool mPressed = false;

        TimeSpan mTimePressed;
        public double mTimeLimit = 1;

        public GUIButton(String iType, Vector2 iPos, Vector2 iDimension)
        {
            mType = iType;
            mPosition = iPos;
            mDimension = iDimension;
        }

        public bool PressButton()
        {
            if (InputHandlerModule.inputHandler.mInput.leftMousePressed)
                if (IsMouseOver())
                    return true;
            return false;
        }

        public bool IsMouseOver()
        {
            Vector2 iMousePos = new Vector2(InputHandlerModule.inputHandler.mInput.mousePosition.X, InputHandlerModule.inputHandler.mInput.mousePosition.Y);
            int halfWidth = (int)(mDimension.X / 2.0f);
            int halfHeight = (int)(mDimension.Y / 2.0f);

            Vector2 pos = mPosition - new Vector2(halfWidth, halfHeight);

            Rectangle rec = new Rectangle((int)pos.X, (int)pos.Y, halfWidth * 2, halfHeight * 2);
            if (rec.Contains((int)iMousePos.X, (int)iMousePos.Y))
                return true;
            else
                return false;
        }

        bool TimePassed(TimeSpan iTime)
        {
            if (!mPressed)
                mTimePressed = iTime;
            else
            {
                double millSec = iTime.Subtract(mTimePressed).TotalSeconds;
                if (millSec > mTimeLimit)
                    return true;
            }
            return false;
        }

//         public double GetTimeToPass(TimeSpan iTime)
//         {
//             double gap = iTime.Subtract(mTimePressed).TotalSeconds;
//             return mTimeLimit - gap;
//         }
    }

    public class GUIChessboardButton
    {
        public Vector2 mPosition;
        public Vector2 mDimension;
        public bool mPressed = false;
        public Tile.Tile mAssociatedTile = null;

        public GUIChessboardButton(Vector2 iPos, Tile.Tile iAssociatedTile, Vector2 iDimension)
        {
            mPosition = iPos;
            mDimension = iDimension;
            mAssociatedTile = iAssociatedTile;
        }

        public bool PressButton()
        {
            if (InputHandlerModule.inputHandler.mInput.leftMousePressed)
                if (IsMouseOver())
                    return true;
            return false;
        }

        public bool IsMouseOver()
        {
            Vector2 iMousePos = new Vector2(InputHandlerModule.inputHandler.mInput.mousePosition.X, InputHandlerModule.inputHandler.mInput.mousePosition.Y);
            int halfWidth = (int)(mDimension.X / 2.0f);
            int halfHeight = (int)(mDimension.Y / 2.0f);

            Vector2 pos = mPosition - new Vector2(halfWidth, halfHeight);

            Rectangle rec = new Rectangle((int)pos.X, (int)pos.Y, halfWidth * 2, halfHeight * 2);
            if (rec.Contains((int)iMousePos.X, (int)iMousePos.Y))
                return true;
            else
                return false;
        }

//         public double GetTimeToPass(TimeSpan iTime)
//         {
//             double gap = iTime.Subtract(mTimePressed).TotalSeconds;
//             return mTimeLimit - gap;
//         }
    }

    public class GUICPButton
    {
        public Vector2 mPosition;
        public Vector2 mDimension;
        public bool mPressed = false;
        public SI.POSITION mType;
        public int mSequencePosition;

        public GUICPButton(Vector2 iPos, SI.POSITION iType, int iSequencePosition, Vector2 iDimension)
        {
            mPosition = iPos;
            mDimension = iDimension;
            mType = iType;
            mSequencePosition = iSequencePosition;
        }

        public bool PressButton()
        {
            if (InputHandlerModule.inputHandler.mInput.leftMousePressed)
                if (IsMouseOver())
                    return true;
            return false;
        }

        public bool IsMouseOver()
        {
            Vector2 iMousePos = new Vector2(InputHandlerModule.inputHandler.mInput.mousePosition.X, InputHandlerModule.inputHandler.mInput.mousePosition.Y);
            int halfWidth = (int)(mDimension.X / 2.0f);
            int halfHeight = (int)(mDimension.Y / 2.0f);

            Vector2 pos = mPosition - new Vector2(halfWidth, halfHeight);

            Rectangle rec = new Rectangle((int)pos.X, (int)pos.Y, halfWidth * 2, halfHeight * 2);
            if (rec.Contains((int)iMousePos.X, (int)iMousePos.Y))
                return true;
            else
                return false;
        }

//         public double GetTimeToPass(TimeSpan iTime)
//         {
//             double gap = iTime.Subtract(mTimePressed).TotalSeconds;
//             return mTimeLimit - gap;
//         }
    }

    public class GUIChessboard
    {
        public GUIChessboardButton[] mChessboard;
        public GUIChessboardButton mRemainedTile;
        public GUICPButton[] mControlPoints;

        public GUIChessboard()
        {
            mChessboard = new GUIChessboardButton[49];
            mControlPoints = new GUICPButton[20];
        }

        public void Init(float iTileLenght, Tile.Tile iRemainedTile, float iViewportWidth, float iViewportheight)
        {
            LabyrinthGameState.State iState = LabyrinthGameState.gameState;
            float startHeight = 0;

            for (int i = 0; i < 7; ++i)
                for (int j = 0; j < 7; ++j)
                {
                    Tile.Tile tile = iState.mChessboard.board[i * 7 + j];
                    Vector2 position = new Vector2(j * iTileLenght + iTileLenght * 1.5f, startHeight + i * iTileLenght + iTileLenght);
                    position += new Vector2(tile.mDimension * 0.5f, tile.mDimension * 0.5f);
                    mChessboard[i * 7 + j] = new GUIChessboardButton(position, tile, new Vector2(iTileLenght, iTileLenght));
                }

            Vector2 offset = new Vector2(iTileLenght * 0.5f, 0);

            for (int i = 0; i < 5; ++i)
                {
                    Vector2 position = new Vector2(2 * iTileLenght + i * iTileLenght, startHeight);
                    position += new Vector2(iTileLenght * 0.5f, iTileLenght * 0.5f);
                    position += offset;
                    mControlPoints[i] = new GUICPButton(position, SI.POSITION.Up, i, new Vector2(iTileLenght, iTileLenght));
                }

                for (int i = 5; i < 10; ++i)
                {
                    int counter = i - 5;
                    Vector2 position = new Vector2(2 * iTileLenght + counter * iTileLenght, iViewportheight - iTileLenght);
                    position += new Vector2(iTileLenght * 0.5f, iTileLenght * 0.5f);
                    position += offset;
                    mControlPoints[i] = new GUICPButton(position, SI.POSITION.Down, counter, new Vector2(iTileLenght, iTileLenght));
                }

                for (int i = 10; i < 15; ++i)
                {
                    int counter = i - 10;
                    Vector2 position = new Vector2(startHeight, startHeight + 2 * iTileLenght + iTileLenght * counter);
                    position += new Vector2(iTileLenght * 0.5f, iTileLenght * 0.5f);
                    position += offset;
                    mControlPoints[i] = new GUICPButton(position, SI.POSITION.Left, counter, new Vector2(iTileLenght, iTileLenght));
                }

                for (int i = 15; i < 20; ++i)
                {
                    int counter = i - 15;
                    Vector2 position = new Vector2(startHeight + iTileLenght * 8, startHeight + 2 * iTileLenght + iTileLenght * counter);
                    position += new Vector2(iTileLenght * 0.5f, iTileLenght * 0.5f);
                    position += offset;
                    mControlPoints[i] = new GUICPButton(position, SI.POSITION.Right, counter, new Vector2(iTileLenght, iTileLenght));
                }

            float xPos = iViewportWidth - iTileLenght * 2.5f;
            float yPos = iViewportheight / 3.5f;
            Vector2 pos = new Vector2(xPos, yPos);
            mRemainedTile = new GUIChessboardButton(pos, iRemainedTile, new Vector2(iTileLenght * 2, iTileLenght * 2));
        }

        public GUIChessboardButton UpdateSelectedTile(GameTime iTime)
        {
            foreach (GUIChessboardButton b in mChessboard)
            {
                if (b.PressButton())
                    return b;
            }
            return null;
        }

        public GUICPButton UpdateCPButton(GameTime iTime)
        {
            foreach (GUICPButton b in mControlPoints)
            {
                if (b.PressButton())
                    return b;
            }
            return null;
        }

        public void UpdateAssociatedTiles()
        {
            LabyrinthGameState.State iState = LabyrinthGameState.gameState;
            for (int i = 0; i < 7; ++i)
                for (int j = 0; j < 7; ++j)
                {
                    Tile.Tile tile = iState.mChessboard.board[i * 7 + j];
                    mChessboard[i * 7 + j].mAssociatedTile = tile;
                }
            mRemainedTile.mAssociatedTile = iState.mRemainedTile;
        }

        public GUICPButton GetSelectedCP()
        {
            foreach (GUICPButton b in mControlPoints)
            {
                if (b.mPressed)
                    return b;
            }
            return null;
        }
    }
}
